//
//  JSSearchBar.swift
//  JS weibo
//
//  Created by Jackie on 16/5/30.
//  Copyright © 2016年 Jackie. All rights reserved.
//

import UIKit

class JSSearchBar: UIView {
    
    @IBOutlet weak var searchTF: UITextField!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    @IBOutlet weak var rightCons: NSLayoutConstraint!
    
    class func loadSearchBar() -> JSSearchBar {
        let searchView = NSBundle.mainBundle().loadNibNamed("JSSearchBar", owner: nil, options: nil).last as! JSSearchBar
        return searchView
    }
    
    override func awakeFromNib() {
        
         self.bounds.size.width = ScreenWidth
        
        searchTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        searchTF.leftViewMode = .Always
        
        searchTF.leftView?.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
        
        searchTF.leftView?.contentMode = .Center
        
    }
    
    @IBAction func textFieldBeginEditing(sender:AnyObject) {
        
        UIView.animateWithDuration(0.25) {
            self.rightCons.constant = self.cancelBtn.bounds.width
            
            self.layoutIfNeeded()
        }
    }
    
    @IBAction func cancelBtnDidClick(sender:AnyObject){
        self.rightCons.constant = 0
        
        UIView.animateWithDuration(0.25) {
        
            self.layoutIfNeeded()
        }
        self.endEditing(true)
    }
}

    

